var id;
var groupid;
var iconid;
var subid;
var menuid;
var classOneArr = []; // 商品一级分类

$(function() {
  groupid = ly.getUrlParam("groupid");
  iconid = ly.getUrlParam("iconid");
  subid = ly.getUrlParam("subid");
  menuid = ly.getUrlParam("menuid");

  urlType = ly.getUrlParam("type");
  id = ly.getUrlParam("id");
  $('.whiteboard').remove();

  // 修改
  if (urlType == 2) {
    $(".title span").html("编辑");
    $(".form").getFormTwo({
      id: id,
      type: "get"
    }, function(data) {
      // $("#storeType").getSelect({
      //   type: "radio",
      //   asName: "storeType",
      //   list: [{ "value": "0", "display": "企业商城" }, { "value": "1", "display": "农产品网" }],
      //   default: data.storeType
      // })
      getClassOneList(data.menuId); // 获取商品一级分类
    });
  // 新增
  } else {
    getClassOneList(); // 获取商品一级分类
    // $("#storeType").getSelect({
    //   type: "radio",
    //   asName: "storeType",
    //   list: [
    //     { value: "0", display: "企业商城" },
    //     { value: "1", display: "农产品网" }
    //   ],
    //   default: 1
    // });
  }

  //提交
  $("body").on("click", "#save-btn", function(event) {
    event.preventDefault();
    var success = $('.form').validate('submitValidate');
    if (success) {
      //验证通过提交表单
      submitFom();
    }
  })

});

// 获取商品一级分类
function getClassOneList(defaultId) {
  request({
    url: site.apiUrl + '/classify/getClassGoods',
    type: "post",
    data: {
      storeType: 1
    },
    callback: function(f) {
      if (f.code == 200) {
        var forData = f.data
        for (var i = 0; i < forData.length; i++) {
          classOneArr.push({
            value: forData[i].id,
            display: forData[i].name
          })
        }
        $("#menuId").getSelect({
          asName: "menuId",
          type: "select",
          list: classOneArr,
          default: defaultId
        });
      }
    }
  })
}

// 上传图片地址集合
function upImgAddress(ele) {
  var imgAddress = [];
  var $imgOne = $(ele).find('.upload-state-done');
  $imgOne.each(function(){
    var imgUrl = $(this).find('input[type=hidden]').val();
    imgAddress.push(imgUrl);
  });
  imgAddress = imgAddress.toString();
  return imgAddress;
}

/*
 * 提交表单
 */
function submitFom() {
  var imgUrl = $("#imgUrl .upload-state-done").find("input[type=hidden]").val();
  var menuId = $("#menuId").val();
  // var storeType = $("#storeType").find('input[name=storeType]:checked').val();

  var obj = {};
  var url = site.apiUrl + "/classify/saveBanner";
  if (urlType == 1) {
    //新增
    obj = {
      'imgUrl': imgUrl,
      'menuId': menuId,
      'storeType': 1
    };
  } else {
    //修改
    var thisId = $("#currentId").val();
    obj = {
      'bannerId': thisId,
      'id': thisId,
      'imgUrl': imgUrl,
      'menuId': menuId,
      'storeType': 1
    };
  }
  request({
    url: url,
    data: obj,
    type: "post",
    callback: function(f) {
      if (f.code == 200) {
        if (urlType == 1) {
          ly.alert('保存成功', function() {
            ly.goto("list.html");
          });
        } else {
          ly.alert('修改成功', function() {
            ly.goto("list.html");
          });
        }
      } else {
        ly.toast(f.msg);
      }
    }
  });
}
